package com.itheima.stock.service.Impl;

import com.itheima.stock.entity.StockBusiness;
import com.itheima.stock.entity.StockMarketIndexInfo;
import com.itheima.stock.entity.StockMarketLogPrice;
import com.itheima.stock.mapper.StockBusinessMapper;
import com.itheima.stock.mapper.StockMarketIndexInfoMapper;
import com.itheima.stock.mapper.StockMarketLogPriceMapper;
import com.itheima.stock.properties.MarketIndexProperties;
import com.itheima.stock.service.StockService;
import com.itheima.stock.util.DateTimeUtil;
import com.itheima.stock.vo.InnerMarketVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class StockServiceImpl implements StockService {
    @Autowired
    private StockMarketLogPriceMapper stockMarketLogPriceMapper;
    @Autowired
    private StockMarketIndexInfoMapper stockMarketIndexInfoMapper;
    @Autowired
    private MarketIndexProperties marketIndexProperties;

    private final StockBusinessMapper stockBusinessMapper;

    public StockServiceImpl(StockBusinessMapper stockBusinessMapper) {
        this.stockBusinessMapper = stockBusinessMapper;
    }


    @Override
    public List<StockBusiness> selectAll() {
        return stockBusinessMapper.selectAll();
    }

    @Override
    public List<InnerMarketVO> increase() {
        // 1. 获取国内大盘指数编码集合
        List<String> inner = marketIndexProperties.getInner();
        // 2. 获取最近的有效的股票交易时间
        LocalDateTime last = DateTimeUtil.getLastDateTime4Stock(LocalDateTime.now());
        //数据不是最新的先用假数据
        String mockDate = "20211226105600";
        last = LocalDateTime.parse(mockDate, DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));

        // 3. 查询
        // 3.1 根据大盘编码和日期查询 股票大盘数据详情表
        List<StockMarketIndexInfo> marketIndexInfoList = stockMarketIndexInfoMapper.selectByIdAndTime(inner,last);
        // 3.2 根据大盘编码和日期查询 股票大盘 开盘价与前收盘价流水表
        List<StockMarketLogPrice> marketLogPriceList = stockMarketLogPriceMapper.selectByIdAndTime(inner,last);

        //4. 组装数据
        return marketIndexInfoList.stream().map((info)->{
            InnerMarketVO build = InnerMarketVO.builder()
                    .code(info.getMarkId())
                    .name(info.getMarkName())
                    .curDate(info.getCurTime().toLocalDate().toString())
                    .tradeAmt(info.getTradeAccount())
                    .tradeVol(info.getTradeVolume())
                    .tradePrice(info.getCurrentPrice())
                    .upDown(info.getUpdownRate())
                    .build();
            marketLogPriceList.forEach((price)->{
                if(price.getMarketCode().equals(info.getMarkId())){
                    build.setOpenPrice(price.getOpenPrice());
                    build.setPreClosePrice(price.getPreClosePrice());
                }else {
                    build.setOpenPrice(BigDecimal.ZERO);
                    build.setPreClosePrice(BigDecimal.ZERO);
                }
            });
            return build;
        }).toList();
    }
}
